# coding=utf-8
from qiniu import Auth, put_data
import logging
from flask import jsonify

from ihome_tenancy.utils.response_code import RET


def put_qiniu(f1):
    # 需要填写你的 Access Key 和 Secret Key
    access_key = 'v3n5FPnRvLujRroKe3X9FxjMSgtWCmMWBeharusu'
    secret_key = 'd3uJUlaQ9iKh0b5XkhU-Tlx5hjrDiWDDeHA4xotw'

    # 空间名称
    bucket_name = 'info11'
    try:
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 生成上传 Token
        token = q.upload_token(bucket_name)
        # 上传文件数据，ret是字典，键为hash、key，值为新文件名，info是response对象
        ret, info = put_data(token, None, f1.read())
        return ret.get('key')
    except:
        logging.ERROR(u'访问七牛云出错')
        return jsonify(code=RET.SERVERERR)
